package org.bleep.util;

import org.lwjgl.opengl.GL11;

import java.io.IOException;

import org.bleep.geom.Rectangle;

public final class SpriteSheet {
	private Texture texture;
	private float imageWidth, imageHeight;
	
	public SpriteSheet(String ref) throws IOException
	{
		texture = TextureManager.getUnfilteredTexture(ref, ref);
		imageWidth = (int)texture.getImageWidth();
		imageHeight = (int)texture.getImageHeight();
	}
	
	public int getImageWidth()
	{
		return (int)imageWidth;
	}
	
	public int getImageHeight()
	{
		return (int)imageHeight;
	}
	
	public void draw(Rectangle bounds)
	{
		float minX = bounds.getX() / imageWidth;
		float minY = bounds.getY() / imageHeight;
		float maxX = (bounds.getX() + bounds.getWidth()) / imageWidth;
		float maxY = (bounds.getY() + bounds.getHeight()) / imageHeight;
		
		texture.bind();
		GL11.glColor4f(1f, 1f, 1f, 1f);
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(minX, minY); // top left
			GL11.glVertex2f(0, 0);
			GL11.glTexCoord2f(maxX , minY); // top right
			GL11.glVertex2f(bounds.getWidth(), 0);
			GL11.glTexCoord2f(maxX, maxY); // bottom right
			GL11.glVertex2f(bounds.getWidth(), bounds.getHeight());
			GL11.glTexCoord2f(minX, maxY); // bottom left
			GL11.glVertex2f(0, bounds.getHeight());
		GL11.glEnd();
	}
}